package com.ftp.web.spider.utils;

import org.apache.commons.codec.binary.Hex;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class CountFileMd5 {

    public static void main(String[] args)  {
        System.out.println("/Users/fengtiepeng/Downloads/abw-023/abw-023.mp4");
        List<String> list=new ArrayList<>();
        list.add("/Users/fengtiepeng/Downloads/abw-023/abw-023.mp4");
        list.add("/Users/fengtiepeng/Downloads/??????www.ygdy8.com.????????3.2021.HD.1080P.??????????.mkv/??????www.ygdy8.com.????????3.2021.HD.1080P.??????????.mkv");
        for(String s:list){
            count(s);
        }
    }

    public static String count(String filePath){
        char[] strMd5=null;
        File file=new File(filePath);
        FileInputStream fileInputStream=null;
        try{
            MessageDigest MD5 = MessageDigest.getInstance("MD5");
            fileInputStream = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                MD5.update(buffer, 0, length);
            }
            strMd5=Hex.encodeHex(MD5.digest());
            //System.out.println(Hex.encodeHex(MD5.digest()));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(fileInputStream!=null){
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.valueOf(strMd5);
    }
}
